<?php
class Message extends AppModel 
{
	var $name = 'Message';

	var $validate = array(
		'name' => array(
			'notEmpty' => array(
				'rule' => 'notEmpty',
				'message' => 'Naam niet ingevuld.'
			),
			//'notEmail' => array(
			//	'rule' => 'notEmail',
			//	'message' => 'Naam mag geen email zijn.'
			//)
		),
		'website' => array(
			'website' => array(
				'rule' => 'website',
				'message' => 'Dit is geen goede website url. Controleer of er \'http://\' voor staat.'
			),
			//'spamsite' => array(
			//	'rule' => 'spamsite',
			//	'message' => 'Dit is een spamsite.'
			//)
		),
		'body' => array(
			'notEmpty' => array(
				'rule' => 'notEmpty',
				'message' => 'Bericht niet ingevuld.'
			),
			//'hasHtml' => array(
			//	'rule' => 'hasHtml',
			//	'message' => 'Er mag geen html in je bericht.'
			//)
		),
        'check' => array(
            'rule' => array('maxLength', 0),
            'message' => 'Spam!'
        )
	);
	
	var $virtualFields = array(
		'date' => 'DATE_FORMAT(created, "%e-%c-%Y")'
	);
	
	function website($check)
	{
		if(!empty($check['website']))
		{
			return preg_match("/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i",$check['website']);
		} else
		{
			return true;
		}
	}
	
	function notEmail($check)
	{
		$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
		$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
		$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
				'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
		$quoted_pair = '\\x5c[\\x00-\\x7f]';
		$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
		$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
		$domain_ref = $atom;
		$sub_domain = "($domain_ref|$domain_literal)";
		$word = "($atom|$quoted_string)";
		$domain = "$sub_domain(\\x2e$sub_domain)*";
		$local_part = "$word(\\x2e$word)*";
		$addr_spec = "$local_part\\x40$domain";
		return !preg_match("!^$addr_spec$!", $check['name']);
	}
	
	function hasHtml($check)
	{
		//return preg_match("/<a href=\\\"([^\\\"]*)\\\">(.*)<\\/a>/iU", $check['body']);
		return true;
	}
	
	function spamsite($check)
	{
		//echo preg_match("/dm8.biz/i", $check['website']);
		return !preg_match("/dm8.biz/i", $check['website']);
	}
	
	function getData()
	{
		$pageNumber = (isset($_GET['pagina'])) ? $_GET['pagina'] : 1;
		$pageNumber = (is_numeric($pageNumber)) ? $pageNumber : 1;
		
		$messagesPerPage = 13;
		
		$messagesTotal = $this->find('count');
		
		$pageCount = ceil($messagesTotal / $messagesPerPage);
		$pageNumber = ($pageNumber > $pageCount) ? $pageCount : $pageNumber;
		
		$MessagePerPage = ($pageNumber == $pageCount) ? $messagesTotal % $messagesPerPage : $pageCount;
		$shoutbox['Messages'] = $this->find('all', array(
			'fields' => array('name', 'body', 'website', 'date'),
			'conditions' => 'active = 1',
			'order' => array('Message.Created DESC'), 
			'limit' => $messagesPerPage, 
			'page' => $pageNumber
		));
		$shoutbox['next'] = $pageNumber + 1;
		$shoutbox['previous'] = $pageNumber - 1;
		$shoutbox['pageCount'] = $pageCount;
		return $shoutbox;
	}
}
?>
